<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/22
 * Time: 9:27
 */

namespace App\Services;


use App\Models\Car;
use App\Models\DriverOthers;
use App\Models\DriverWallets;
use App\Models\Orders;
use App\Models\User;
use App\Models\Version;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class DriversIndexService extends BaseService
{
    /**
     * 首页订单统计
     */
    public function index_order_count(User $user)
    {
        $driver_id = $user->driver()->first() ? $user->driver()->first()->id : 0;
        $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        $count = Orders::query()
            ->select(DB::raw("sum(minute) as minute,sum(km) as km,count(id) as count ,sum(driver_money) as driver_money"))
            ->where('created_at', '>', date('Y-m-d 00:00:00', time()))
            ->where('created_at', '<', date('Y-m-d 23:59:59', $endToday))
            ->where('driver_id', $driver_id)
            ->whereIn('status', ['4', '5', '6'])
            ->get();
        $amount = DriverWallets::query()->where([['driver_id', $driver_id], ['type', '0']])->first('amount');
        $driving = DriverWallets::query()->where([['driver_id', $driver_id], ['type', '1']])->first('amount');
        $count[0]['driver_id'] = $driver_id; //司机ID
        $count[0]['online'] = $amount ? $amount->amount : 0; //网约车钱包
        $count[0]['driving'] = $driving ? $driving->amount : 0; //网约车钱包
        if (is_null($count[0]['driver_money'])) $count[0]['driver_money'] = 0;
        if (is_null($count[0]['minute'])) $count[0]['minute'] = 0;
        return $count[0];
    }

    /**
     * 选单接口
     * @param $lat
     * @param $lng
     * @param User $user
     */
    public function driverMenu(User $user, int $car_id, string $types)
    {
        if ($types == '0') {
            $company_id = $user->driver()->first()->company_id; //城市ID
            if (!$company_id) return_bobc('公司不存在');
        } else {
            $company = DriverOthers::query()->first('company_id'); //公司ID
            $company_id = $company ? $company->company_id : 0;
        }
        $city = \App\Models\ServiceCity::query()->where('default_driver_company_id', $company_id)->first('city_id');
        $city_id = $city->city_id;
        $type = Car::query()->where('id', $car_id)->first('type');
        $type_id = $type ? $type->type : '';
        return $this->drivingverMenu($type_id, $types, $city_id);
    }

    /**
     * 选单接口 网约车
     */
    public function drivingverMenu($type_id, $types, $city_id)
    {
        $sql = Orders::query()->where('status', '1')
            ->where('city_id', $city_id)
            ->where('type', $types)
            ->select(['id', 'created_at'])
            ->orderByDesc('created_at');
        // ->whereTime('created_at', '<', date('Y-m-d H:i:s', time() - 60));
        if ($types == '0') {
            $list = $sql->where('call_service_type_id', 'like', '%' . $type_id . '%')
                ->count();
        } else {
            $list = $sql->count();
        }
        if (!$list) $list = 0;
        return $list;
    }

    /**
     * @param $version
     * 版本更新
     */
    public function version_status(int $version)
    {
        $version = Version::query()->select(['version', 'url', 'is_update'])->where('version', '>', $version)->first();
        if ($version) {
            return $version;
        }
        return null;
    }
}